<?php 

require_once("ui.php");

define("NOT_USED_RADIO_VALUE", -1);
define("NOT_USED_ID", "noUse");

define("MAX_ITEMS_ON_PAGE", 10);

class TableItem {
	public $itemId;
	public $tabId;
	public $description;
	public $usageOptions;
	public $categoryId;
	public $pageId = "";
	
	function __construct($itemId, $description, $usageOptions, $tabId, $categoryId) {
		$this->itemId = $itemId;
		$this->description = $description;
		$this->usageOptions = $usageOptions; 
		$this->tabId = $tabId;
		$this->categoryId = $categoryId;
	}

	public function setPageId($pageId) {
		$this->pageId = $pageId;
	}
	
	public function getRadioId($usageId) {
		return "radio" . $this->tabId . "-" . $this->categoryId . "-" . $this->pageId .
			"-" . $this->itemId . "-" . $usageId; 
	}
	
	public function getRadioGroupName() {
		return "radioGroup" . $this->tabId . "-" . $this->categoryId . "-" . $this->pageId .
			"-" . $this->itemId;
	}
	
	private function printUsageButton($usageId, $checked) {
		echo '<td class="radioButtonTableCell">';
		
		$radioId = $this->getRadioId($usageId);
		$radioGroupName = $this->getRadioGroupName();
		
		echo '<div class="tableRadioButtonDiv" onclick=\'document.getElementById("' . $radioId . '").checked=true;\'>';
		
		printRadio($radioId, "usageRadio", $radioGroupName, $usageId, $checked);
		
		echo '</div>';
		echo '</td>';
	}

	public function getRowId() {
		return "row" . $this->tabId . "-" . $this->categoryId . "-" . $this->pageId .
			"-" . $this->itemId;
	}
	
	public function printItemContent($odd) {			
		$rowClassname = $odd ? "usageTableOddRow" : "";
		
		echo '<tr class="' . $rowClassname . '"id="' . $this->getRowId() . '">';
		echo '<td class="usageTableFirstColumn">' . $this->description . '</td>';

		$previousValue = getOriginalPost($this->getRadioGroupName());

		$defaultCheck = true;
		foreach ($this->usageOptions as $i => $usageOption) {
			if ($previousValue == $usageOption->usageId) {
				$defaultCheck = false;
				break;
			}
		}			
		
		$this->printUsageButton(NOT_USED_ID, $defaultCheck);			
		
		foreach ($this->usageOptions as $i => $usageOption) {
			$checked = ($previousValue == $usageOption->usageId);
			$this->printUsageButton($usageOption->usageId, $checked);			
		}			

		echo '</tr>';
	}
}

class TablePage {
	public $pageId;
	public $items;
	public $tabId;
	public $categoryId;
	
	function __construct($pageId, $items, $tabId, $categoryId) {
		$this->pageId = $pageId;
		$this->items = $items;
		$this->tabId = $tabId;
		$this->categoryId = $categoryId;

		foreach ($this->items as $i => $item) {
			$item->setPageId($this->pageId);
		}
	}
		
	public function printPageContent() {		
		foreach ($this->items as $i => $item) {
			$oddRow = ($i % 2 != 0);
			$item->printItemContent($oddRow);
		}
	}	
}

class TableCategory {
	public $tabId;
	public $categoryId;
	public $description;
	public $items;
	public $pages;
	
	function __construct($categoryId, $description, $items, $tabId) {
		$this->tabId = $tabId;
		$this->categoryId = $categoryId;
		$this->items = $items;
		$this->pages = $this->createPages($items);
		$this->description = $description;
	}	
	
	private function createPages($items) {
		
		$pageCount = ceil(count($items) / MAX_ITEMS_ON_PAGE);		
		$createdPages = array();
		
		for ($i = 0; $i < $pageCount; $i++) {
			$firstItemsIndex = $i * MAX_ITEMS_ON_PAGE;
			$pageItems = array_slice($items, $firstItemsIndex, MAX_ITEMS_ON_PAGE);
			
			array_push($createdPages, new TablePage($i, $pageItems, $this->tabId, $this->categoryId));
		}
		
		return $createdPages;
	}
	
	public function printCategoryContent() {
		foreach ($this->pages as $i => $page) {
			$page->printPageContent();
		}
	}
}

class TableTab {

	public $tabId;
	public $usageOptions;
	public $categories;
	
	function __construct($tabId, $usageOptions, $categories) {
		$this->tabId = $tabId;
		$this->usageOptions = $usageOptions;
		$this->categories = $categories;
	}	
	
	public function getSelectId() {
		return "select" . $this->tabId; 
	}
	
	public function printHeader() {
		echo '<tr class="usageTableFirstColumn">';
		echo '<th>Category:';
		
		$optionsInfo = array();

		foreach ($this->categories as $i => $category) {
			$singlePage = (count($category->pages) == 1);
			
			foreach ($category->pages as $j => $page) {
				$description = $category->description;
				if (!$singlePage) {
					$description .= " " . ($j + 1);
				}
				
				$optionInfo['value'] = $category->categoryId . " " . $page->pageId;
				$optionInfo['description'] = $description;
				
				array_push($optionsInfo, $optionInfo);				
			}
		}
		
		printSelect($this->getSelectId(),
			"selectUsagePage",
			'"' . $this->tabId . '"',
			$optionsInfo,
			null,
			"");		
				
		echo '</th>';
		echo '<th class="usageHeader">Not used</th>';
		
		foreach ($this->usageOptions as $i => $usageOption) {
			echo '<th class="usageHeader">' . $usageOption->description . '</th>';
		}
			
		echo '</tr>';		
	}
	
	public function printTabContent() { 
		echo '<div class="reportTab" id="' . $this->tabId . '">';
		echo '<table class="reportTable">';
					
		$this->printHeader();
		
		echo '<tbody>';
		foreach ($this->categories as $i => $category) {
			$category->printCategoryContent();
		}
					
		echo '</tbody>';
		echo '</table>';
	
		echo '
			<div class="formTableButtonsDiv">
				<div id="reportTableNavButton" class="formButton" onclick=\'moveTableCategory("' . $this->tabId . '", true);\'><a href="#">&#62;&#62;</a></div>
				<div id="reportTableNavButton" class="formButton" onclick=\'moveTableCategory("' . $this->tabId . '", false);\'><a href="#">&#60;&#60;</a></div>
			</div>';				
		
		echo '</div>';
	}	
}

?>